package com.imooc.bilibili.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.bilibili.domain.user.RefreshToken;
import com.imooc.bilibili.exception.ConditionException;
import com.imooc.bilibili.mapper.user.RefreshTokenMapper;
import com.imooc.bilibili.user.service.IRefreshTokenService;
import com.imooc.bilibili.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 刷新令牌记录表 服务实现类
 * </p>
 *
 * @author gcq
 * @since 2022-06-03
 */
@Service
public class RefreshTokenServiceImpl extends ServiceImpl<RefreshTokenMapper, RefreshToken> implements IRefreshTokenService {

    @Autowired
    private RefreshTokenMapper refreshTokenMapper;

    @Override
    public String refreshAccessToken(String refreshToken) throws Exception {
        RefreshToken tokenDetail = refreshTokenMapper.selectOne(new QueryWrapper<RefreshToken>().eq("refreshToken", refreshToken));
        if(tokenDetail == null){
            throw new ConditionException("555", "token已过期");
        }
        return TokenUtil.generateToken(tokenDetail.getUserId());
    }
}
